package com.zry.simpleBlog.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.zry.simpleBlog.entity.User;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.Email;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;

/**
 * @author zry
 * @create 2022-04-10 22:54
 */
@Data
@NoArgsConstructor
public class UserDTO implements Serializable {

    public static UserDTO valueOf(User user) {
        UserDTO userDto = new UserDTO();
        if (user == null) {
            return userDto;
        }
        userDto.id = user.getId();
        userDto.avatar = user.getAvatar();
        userDto.email = user.getEmail();
        userDto.nickname = user.getNickname();
        userDto.rank = user.getRank();
        userDto.username = user.getUsername();
        userDto.introduction = user.getIntroduction();
        userDto.createTime = user.getCreateTime();
        userDto.updateTime = user.getUpdateTime();
        return userDto;
    }

    public User castUser() {
        User user = new User();
        user.setId(this.id);
        user.setAvatar(this.avatar.trim());
        user.setEmail(this.email.trim());
        user.setNickname(this.nickname.trim());
        user.setUsername(this.username.trim());
        user.setRank(this.rank);
        user.setIntroduction(this.introduction);
        user.setCreateTime(this.createTime);
        user.setUpdateTime(this.updateTime);
        return user;
    }

    private String id;

    private String avatar;

    @Email(message = "邮箱格式不正确")
    private String email;

    private String nickname;

    private Integer rank;

    private String introduction;

    @Size(message = "用户名应为2-20个字符",min = 2, max = 20)
    private String username;

    @JsonFormat(pattern = "yyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;
    @JsonFormat(pattern = "yyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateTime;

}
